package controller

import (
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
	"wms/define"
	"wms/logic"
)

// GetMenuListHandler 获取菜单列表
// @Summary 获取菜单列表接口
// @Description 认证后可拉取菜单列表
// @Tags 菜单相关接口
// @Accept application/json
// @Produce application/json
// @Param Authorization header string true "Bearer 用户令牌"
// @Success 200 {object} define.DocResponseData
// @Router /settings/menu/get/list [get]
func GetMenuListHandler(c *gin.Context) {
	mc := c.MustGet("MyClaims").(*define.MyClaims)

	data := make([]*define.MenuReply, 0)
	if err := logic.GetMenuList(mc, &data); err != nil {
		zap.L().Error("logic.GetMenuList failed", zap.Error(err))
		define.ResponseError(c, define.CodeServerBusy)
		return
	}

	define.ResponseSuccess(c, define.CodeSuccessGetMenus, gin.H{
		"menu": data,
	})
	return
}

// CreateMenuHandler 添加菜单
// @Summary 创建菜单接口
// @Description 鉴权后用户需要提交菜单基本信息创建菜单
// @Tags 菜单相关接口
// @Accept application/json
// @Produce application/json
// @Param object body define.ParamMenuAdd true "菜单信息"
// @Param Authorization header string true "Bearer 用户令牌"
// @Success 200 {object} define.DocResponseData
// @Router /settings/menu/create [post]
func CreateMenuHandler(c *gin.Context) {
	p := new(define.ParamMenuAdd)
	if err := c.ShouldBindJSON(p); err != nil {
		zap.L().Error("DevMenuAdd bind param failed", zap.Error(err))
		define.ResponseError(c, define.CodeInvalidParam)
		return
	}

	if err := logic.AddOneMenu(p); err != nil {
		zap.L().Error("logic.AddOneMenu failed", zap.Error(err))
		define.ResponseError(c, define.CodeServerBusy)
		return
	}

	define.ResponseSuccess(c, define.CodeSuccessAddMenu, nil)
	return
}

// DeleteMenuHandler 删除菜单
// @Summary 删除菜单接口
// @Description 鉴权后用户需要提交菜单唯一标识删除菜单
// @Tags 菜单相关接口
// @Accept application/json
// @Produce application/json
// @Param Identity query string true "菜单标识"
// @Param Authorization header string true "Bearer 用户令牌"
// @Success 200 {object} define.DocResponseData
// @Router /settings/menu/delete [delete]
func DeleteMenuHandler(c *gin.Context) {
	identity := c.Query("identity")
	// 参数为空，必须填写
	if identity == "" {
		define.ResponseError(c, define.CodeInvalidParam)
		return
	}

	if err := logic.DelOneMenu(identity); err != nil {
		zap.L().Error("logic.DelOneMenu failed", zap.Error(err))
		define.ResponseError(c, define.CodeServerBusy)
		return
	}

	define.ResponseSuccess(c, define.CodeSuccessDelMenu, nil)
}

// UpdateMenuHandler 更新菜单
// @Summary 更新菜单接口
// @Description 鉴权后用户需要提交菜单基本信息更新菜单
// @Tags 菜单相关接口
// @Accept application/json
// @Produce application/json
// @Param object body define.ParamMenuUpdate true "菜单信息"
// @Param Authorization header string true "Bearer 用户令牌"
// @Success 200 {object} define.DocResponseData
// @Router /settings/menu/update [put]
func UpdateMenuHandler(c *gin.Context) {
	p := new(define.ParamMenuUpdate)
	if err := c.ShouldBindJSON(p); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("DevMenuUpdate bind param failed", zap.Error(err))
			define.ResponseError(c, define.CodeInvalidParam)
			return
		}
		zap.L().Error("DevMenuUpdate bind param failed, tag binding error", zap.Error(err))
		define.ResponseErrorWithMsg(c, define.CodeInvalidParam, define.RemoveTopStruct(errs.Translate(define.Trans)))
		return
	}

	if err := logic.UpdateOneMenu(p); err != nil {
		zap.L().Error("logic.UpdateOneMenu failed", zap.Error(err))
		define.ResponseError(c, define.CodeServerBusy)
		return
	}

	define.ResponseSuccess(c, define.CodeSuccessUpdateMenu, nil)
}
